package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Zyf;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.SQLQuery;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Melody on 14-6-24.
 */
@Repository
public class ZyfDao extends SimpleHibernateTemplate<Zyf, Long> {
    public Page<Zyf> findZyfList(String qc, String jc, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Zyf> page = new Page<Zyf>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qc != null && !qc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qc like ? ");
            param.add("%" + qc.trim() + "%");
        }
        if (jc != null && !jc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append("( jc like ? or qc like ?)");
            param.add("%" + jc.trim() + "%");
            param.add("%" + jc.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Zyf ";
        if (condition.length() != 0)
            hql = hql + " where " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Zyf ";
        if (condition.length() != 0)
            hql = hql + " where " + condition + "order by dateCreated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Zyf where " + propertyName + " = ? and type = 'zyf'";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Zyf where " + propertyName + " = ? and id <> ? and type = 'zyf'";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

    public List<Map> findZyfJcList(String jc, int pageNo, int pageSize) {
        String select = "select  g.id  as \"id\",  g.jc  as  \"jc\" ";
        String from = " from t_sys_gsjg g where g.type in ('zyf') and g.deleted = 0";
        StringBuilder where = new StringBuilder();
        if (jc != null) {
            where.append(" and ");
            where.append("g.jc like :jc");
        }
        SQLQuery query = getSession().createSQLQuery(select + from + where);
        if (jc != null) {
            query.setString("jc", "%" + jc.trim() + "%");
        }
        query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
        return query.list();
    }

}
